import 'dart:ui';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:path/path.dart' as path;
import 'package:tiktokadult/assets_tool/images.dart';
import 'package:tiktokadult/config/config.dart';
import 'package:tiktokadult/hj_utils/screen.dart';
import 'package:tiktokadult/tools_base/cache/image_cache_manager.dart';

class NetworkImageLoader extends StatelessWidget {
  final double borderRadius;
  final BorderRadius? imgBorderRadius;
  final String? imageUrl;
  final double? width;
  final double? height;
  final BoxFit fit;
  final double? placeHolderH;
  final double? placeHolderW;
  final bool blur; //高斯模糊
  final Alignment alignment;
  final bool encrypt; //是否需要加密
  final Widget? placeHolderWidget;
  final bool compress; //是否需要压缩
  final int? loadWidth; // 服务端根据宽度等比例压缩
  final bool? isResizeImage; //是否需要根据组件大小压缩

  const NetworkImageLoader({
    super.key,
    required this.imageUrl,
    this.borderRadius = 8,
    this.imgBorderRadius,
    this.height,
    this.fit = BoxFit.cover,
    this.width,
    this.blur = false,
    this.placeHolderH,
    this.placeHolderW,
    this.alignment = Alignment.center,
    this.encrypt = true,
    this.placeHolderWidget,
    this.compress = false,
    this.loadWidth,
    this.isResizeImage = true,
  });

  bool get isGif => imageUrl?.contains(".gif") ?? false;
  String get realImgUrl {
    if (imageUrl == null) return "";
    if (imageUrl!.startsWith("http") || imageUrl!.startsWith("https") || isGif) {
      return imageUrl!;
    } else {
      String prefixPath = "imageView/1";
      if (blur) {
        prefixPath = path.join(prefixPath, "s/${Config.GAUSS_VALUE}");
      }
      if ((loadWidth ?? 0) > 0) {
        prefixPath = path.join(prefixPath, "w/${loadWidth!.floor()}");
      }
      return path.join(prefixPath, imageUrl);
    }
  }

  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(builder: (context, cons) {
      final imageWidget = CachedNetworkImage(
        alignment: alignment,
        imageUrl: realImgUrl,
        width: width,
        height: height,
        fit: fit,
        memCacheWidth: memCacheWidth(cons),
        cacheManager: encrypt ? (compress == true ? ImageCacheCompressManager() : ImageCacheManager()) : null,
        placeholder: (context, url) => _buildPlaceHolder(),
        errorWidget: (context, url, err) => _buildPlaceHolder(),
        fadeInCurve: Curves.linear,
        fadeOutCurve: Curves.linear,
      );
      final clip = borderRadius == 0
          ? imageWidget
          : ClipRRect(
              borderRadius: imgBorderRadius ?? BorderRadius.circular(borderRadius),
              child: imageWidget,
            );
      final backdrop = blur
          ? ClipRRect(
              borderRadius: imgBorderRadius ?? BorderRadius.circular(borderRadius),
              child: ImageFiltered(
                imageFilter: ImageFilter.blur(
                  sigmaX: 5,
                  sigmaY: 5,
                ),
                child: clip,
              ),
            )
          : clip;
      return backdrop;
    });
  }

  //resize的宽度
  int? memCacheWidth(BoxConstraints cons) {
    if (isResizeImage == true) {
      return (cons.maxWidth == double.infinity || cons.maxWidth.isNaN) ? null : (cons.maxWidth * screen.devicePixelRatio).toInt();
    } else {
      return loadWidth != null ? loadWidth! : (Get.width * screen.devicePixelRatio).toInt();
    }
  }

  _buildPlaceHolder() {
    if (placeHolderWidget != null) return placeHolderWidget;
    return Container(
      decoration: BoxDecoration(color: Color(0xff343434)),
      alignment: Alignment.center,
      child: Image.asset(
        'place_holder_logo.webp'.commonImgPath,
        width: placeHolderW ?? 96,
        height: placeHolderW ?? 96,
      ),
    );
  }
}
